package com.tracker.session;

import java.util.Set;

import org.apache.log4j.Logger;
import com.tracker.bean.LoggerPoints;
import com.tracker.dao.LoggerPointsDAO;
import com.tracker.utils.GPSUtils;


public class LoggerPointsSession {
		private static Logger logger = Logger.getLogger("LoggerPointsSession");

		private LoggerPointsDAO loggerPointsDAO = new LoggerPointsDAO();


        public void add(LoggerPoints loggerPoints) throws Exception{
        	GPSUtils utils = new GPSUtils();
            String latitudeDirection = "";
            String longitudeDirection = "";
            String latitude = "";
            String longitude = "";

            try{
//               if(gpsVO.nQuality > 0){

            		latitudeDirection  = utils.getSignalDirection(loggerPoints.getLatitudeDir(), "latitude");
                    longitudeDirection = utils.getSignalDirection(loggerPoints.getLongitudeDir(), "longitude");
                    logger.debug(String.valueOf("Latitude: " + loggerPoints.getLatitude()));
                    logger.debug(String.valueOf("Longitude: " + loggerPoints.getLongitude()));                    
                    latitude  = latitudeDirection  + utils.getGPoint(loggerPoints.getLatitude());
                    longitude = longitudeDirection + utils.getGPoint(loggerPoints.getLongitude());
                    loggerPoints.setLatitude(latitude);
                    loggerPoints.setLongitude(longitude);
                    //reajustDateTime(nTimeZone, gpsDevice, historyUserGps);

                    logger.debug("Date (dd/MM/yyyy) = " + loggerPoints.getDate());
                    logger.debug("Time = " + loggerPoints.getTime());
                    
                	loggerPointsDAO.persist(loggerPoints);

            }catch(Exception ex){
                logger.error(ex.getMessage());
            }
            
        }

        public Set<LoggerPoints> list(String imei, String dateI, String dateF) throws Exception{
        	return loggerPointsDAO.getListObject(imei, dateI, dateF);
        }

        public LoggerPoints get(LoggerPoints loggerPoints) throws Exception{
        	return loggerPointsDAO.getObjectByKey(loggerPoints);
        }
        
        public LoggerPoints getLastCoord(LoggerPoints loggerPoints) throws Exception{
        	return loggerPointsDAO.getLastCoord(loggerPoints);
        }
        
        
        /*
        //Per i modeli gps che restituiscono un RMC con le coordinate giuste
          public void insertCoordinate(List<LoggerPoints> lsCoordinates) throws SQLException{
              logger.debug("insertCoordinate()");        
              LoggerPointsImpl loggerPointsImpl = new LoggerPointsImpl(); 
              for(LoggerPoints loggerPoints : lsCoordinates){
                  try{
                          //int nTimeZone = user.getTimeZone(gpsVO.strIdUser);
                          //reajustDateTime(nTimeZone, gpsVO, historyUserGps);
                          double latitude  = Integer.parseInt(loggerPoints.getLatitude()) * 1.6666666666666E-07;
                          double longitude = Integer.parseInt(loggerPoints.getLongitude()) * 1.6666666666666E-07;
                          loggerPoints.setLatitude(String.valueOf(latitude));
                          loggerPoints.setLongitude(String.valueOf(longitude));

                          loggerPointsImpl.save(loggerPoints);
                       
                  }catch(Exception ex){
                      logger.error(ex.getMessage());
                  }
                  
              }
          }
      */		        
        
}